package com.sc.nft.service.impl;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sc.nft.dao.DaoIdentityDao;
import com.sc.nft.dao.DigitalCollectionDao;
import com.sc.nft.dao.UserCollectionDao;
import com.sc.nft.entity.DaoIdentity;
import com.sc.nft.entity.DigitalCollection;
import com.sc.nft.entity.UserCollection;
import com.sc.nft.entity.vo.MallDaoIdentityVo;
import com.sc.nft.entity.vo.MallUserIdentityVo;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.service.DaoIdentityService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * DAO身份表服务接口实现
 *
 * @author zxy
 * @description
 * @since 2023-10-20 00:56:41
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class DaoIdentityServiceImpl implements DaoIdentityService {

    private final DaoIdentityDao daoIdentityDao;
    private final DigitalCollectionDao digitalCollectionDao;
    private final UserCollectionDao userCollectionDao;

    @Override
    public Page<MallDaoIdentityVo> pageDaoIdentity(Page<DaoIdentity> page, Long daoId, String firstTitle) {
        Page<DaoIdentity> daoIdentityPage = daoIdentityDao.pageByDaoIdAndFirstTitle(page, daoId, firstTitle);

        Page<MallDaoIdentityVo> mallDaoIdentityVoPage = new Page<>();
        mallDaoIdentityVoPage.setCurrent(daoIdentityPage.getCurrent());
        mallDaoIdentityVoPage.setSize(daoIdentityPage.getSize());
        mallDaoIdentityVoPage.setTotal(daoIdentityPage.getTotal());

        List<DaoIdentity> records = daoIdentityPage.getRecords();
        if (CollectionUtils.isNotEmpty(records)) {
            //region 藏品
            List<Long> collectionIdList = records.stream().map(DaoIdentity::getCollectionId).distinct().collect(Collectors.toList());
            List<DigitalCollection> digitalCollectionList = digitalCollectionDao.getByIds(collectionIdList);
            Map<Long, DigitalCollection> digitalCollectionMap = CollectionUtils.isNotEmpty(digitalCollectionList) ? digitalCollectionList.stream().collect(Collectors.toMap(DigitalCollection::getId, Function.identity())) : new HashMap<>();
            //endregion
            List<MallDaoIdentityVo> daoIdentityVos = new ArrayList<>();
            records.forEach(record -> {
                MallDaoIdentityVo mallDaoIdentityVo = new MallDaoIdentityVo();
                mallDaoIdentityVo.setIdentityId(record.getId());
                DigitalCollection collection = digitalCollectionMap.get(record.getCollectionId());
                mallDaoIdentityVo.setFirstTitle(Objects.nonNull(collection) ? collection.getFirstTitle() : record.getFirstTitle());
                mallDaoIdentityVo.setCreationContent(Objects.nonNull(collection) ? collection.getCreationContent() : record.getCreationContent());
                daoIdentityVos.add(mallDaoIdentityVo);
            });
            mallDaoIdentityVoPage.setRecords(daoIdentityVos);
        }

        return mallDaoIdentityVoPage;
    }

    @Override
    public Page<MallUserIdentityVo> pageUserIdentity(Page<DaoIdentity> page, Long userId, List<Long> identityIdList) {
        Assert.notEmpty(identityIdList, () -> new GlobalRunTimeException("权益卡不能为空"));
        LambdaQueryWrapper<DaoIdentity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(DaoIdentity::getId, identityIdList);
        queryWrapper.orderByAsc(DaoIdentity::getId);
        Page<DaoIdentity> daoIdentityPage = daoIdentityDao.page(page, queryWrapper);

        Page<MallUserIdentityVo> mallUserIdentityVoPage = new Page<>();
        mallUserIdentityVoPage.setCurrent(daoIdentityPage.getCurrent());
        mallUserIdentityVoPage.setSize(daoIdentityPage.getSize());
        mallUserIdentityVoPage.setTotal(daoIdentityPage.getTotal());

        List<DaoIdentity> records = daoIdentityPage.getRecords();
        if (CollectionUtils.isNotEmpty(records)) {
            //region 藏品
            List<Long> collectionIdList = records.stream().map(DaoIdentity::getCollectionId).distinct().collect(Collectors.toList());
            List<DigitalCollection> digitalCollectionList = digitalCollectionDao.getByIds(collectionIdList);
            Map<Long, DigitalCollection> digitalCollectionMap = CollectionUtils.isNotEmpty(digitalCollectionList) ? digitalCollectionList.stream().collect(Collectors.toMap(DigitalCollection::getId, Function.identity())) : new HashMap<>();
            //endregion
            //region 用户持有藏品
            List<UserCollection> userCollections = userCollectionDao.getByCollectionIdsAndUserId(collectionIdList, userId);
            Map<Long, List<UserCollection>> userCollectionMap = CollectionUtils.isNotEmpty(userCollections) ? userCollections.stream().collect(Collectors.groupingBy(UserCollection::getCollectionId)) : new HashMap<>();
            //endregion
            List<MallUserIdentityVo> mallUserIdentityVos = new ArrayList<>();
            records.forEach(record -> {
                MallUserIdentityVo mallUserIdentityVo = new MallUserIdentityVo();
                mallUserIdentityVo.setIdentityId(record.getId());
                DigitalCollection collection = digitalCollectionMap.get(record.getCollectionId());
                mallUserIdentityVo.setFirstTitle(Objects.nonNull(collection) ? collection.getFirstTitle() : record.getFirstTitle());
                mallUserIdentityVo.setCreationContent(Objects.nonNull(collection) ? collection.getCreationContent() : record.getCreationContent());
                List<UserCollection> userCollectionsList = userCollectionMap.get(record.getCollectionId());
                mallUserIdentityVo.setHoldCount(CollectionUtils.isNotEmpty(userCollectionsList) ? userCollectionsList.size() : 0);
                mallUserIdentityVos.add(mallUserIdentityVo);
            });
            mallUserIdentityVoPage.setRecords(mallUserIdentityVos);
        }

        return mallUserIdentityVoPage;
    }

}